const {app, Menu, MenuItem} = require('electron');
const commands = require('./commands');
const dialogUtil = require('./utils/dialogUtil.js');

const template = [
  {
    label: '文件',
    submenu: [
      {
        label: '新建',
        command: 'new-file'
      },
      {
        label: '打开',
        command: 'open-file'
      },
      {
        label: '保存',
        command: 'save-file'
      },
      {
        label: '保存为组件',
        command: 'save-as-view'
      }
    ]
  },
  {
    label: 'Edit',
    submenu: [
      {
        role: 'undo'
      }, {
        role: 'redo'
      }, {
        type: 'separator'
      }, {
        role: 'cut'
      }, {
        role: 'copy'
      }, {
        role: 'paste'
      }, {
        role: 'pasteandmatchstyle'
      }, {
        role: 'delete'
      }, {
        role: 'selectall'
      }, {
        type: 'separator'
      }, {
        label: '配置',
        command: 'config'
      }
    ]
  }, {
    label: 'View',
    submenu: [
      {
        role: 'reload'
      }, {
        role: 'forcereload'
      }, {
        role: 'toggledevtools'
      }, {
        type: 'separator'
      }, {
        role: 'resetzoom'
      }, {
        role: 'zoomin'
      }, {
        role: 'zoomout'
      }, {
        type: 'separator'
      }, {
        role: 'togglefullscreen'
      }
    ]
  }, {
    role: 'window',
    submenu: [
      {
        role: 'minimize'
      }, {
        role: 'close'
      }
    ]
  }, {
    role: 'help',
    submenu: [
      {
        label: 'Learn More',
        click() {
          require('electron')
            .shell
            .openExternal('https://electron.atom.io')
        }
      }
    ]
  }
];

if (process.platform === 'darwin') {
  template.unshift({
    label: app.getName(),
    submenu: [
      {
        role: 'about'
      }, {
        type: 'separator'
      }, {
        role: 'services',
        submenu: []
      }, {
        type: 'separator'
      }, {
        role: 'hide'
      }, {
        role: 'hideothers'
      }, {
        role: 'unhide'
      }, {
        type: 'separator'
      }, {
        role: 'quit'
      }
    ]
  });

  // Edit menu
  template[2].submenu.push({
    type: 'separator'
  }, {
    label: 'Speech',
    submenu: [
      {
        role: 'startspeaking'
      }, {
        role: 'stopspeaking'
      }
    ]
  });

  // Window menu
  template[4].submenu = [
    {
      role: 'close'
    }, {
      role: 'minimize'
    }, {
      role: 'zoom'
    }, {
      type: 'separator'
    }, {
      role: 'front'
    }
  ]
}

const menu = new Menu();

template.forEach(item => {
  const menuItem = new MenuItem(item);
  resolveCommand(menuItem);
  menu.append(menuItem);
});

function resolveCommand(menuItem) {
  if (menuItem.command) {
    menuItem.click = () => {
      const command = commands(menuItem.command);
      if (command) {
        command.execute().catch(err => {
          dialogUtil.showError(err);
        });
      }
    }
  }

  if (menuItem.submenu && menuItem.submenu.items.length > 0) {
    menuItem.submenu.items.forEach(subMenuItem => {
      resolveCommand(subMenuItem);
    });
  }
}

Menu.setApplicationMenu(menu);
